package button_tools;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Slave
 */
public class Window extends JFrame
{
    /**
     * Data Types
     */
    private ImageIcon rectangleImg = new ImageIcon("img/rectangle.png");
    private ImageIcon lineImg = new ImageIcon("img/line.png");
    private ImageIcon circleImg = new ImageIcon("img/circle.png");
    private ImageIcon triangleImg = new ImageIcon("img/triangle.png");
    private ImageIcon brushImg = new ImageIcon("img/pbrush.png");


    private JButton rectangle = new JButton(rectangleImg);
    private JButton line = new JButton(lineImg);
    private JButton circle = new JButton(circleImg);
    private JButton triangle = new JButton(triangleImg);
    private JButton brush = new JButton(brushImg);
    private JButton select = new JButton();

    private JRadioButton filled = new JRadioButton();
    private JRadioButton non_filled = new JRadioButton();

    private int x = 0; private int y = 0;
    private JLabel xy = new JLabel("X: " + x + "" + "Y: " + y);
    private JLabel xyText = new JLabel("Coordinates");
    private JLabel toolText = new JLabel("Tools");
    private JLabel colorText = new JLabel("Colors");

    // color labels
    private JPanel red = new JPanel();
    private JPanel green = new JPanel();
    private JPanel blue = new JPanel();
    private JPanel black = new JPanel();

    // thinkness panel
    private JLabel textSize = new JLabel("Size Selection");
    private JPanel sizeOne = new JPanel();
    private JPanel sizeTwo = new JPanel();
    private JPanel sizeThree = new JPanel();
    boolean wndMoved = false;
   // int X1 = 570; int X2 = 370;
    boolean mX1 = false; boolean mX2 = false; boolean mY1 = false; boolean mY2 = false;
    
    TPaintSettings m_Settings;

    /**
     * Constructor
     */
    public Window(TPaintSettings paintSettings)
    {
        super("Paint"); // frame title
        m_Settings = paintSettings;
        setLayout(null); // this will null out the frame from trying to get a layout
        setSize(800,600); // size of frame
        setVisible(true); // set visible
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // force close
        final TDraw_Panel drawArea = new TDraw_Panel(m_Settings); // create a new draw area which will be placed on the frame
        final Color_Panel colorArea = new Color_Panel(); // create a new color area which will be placed on the frame

        this.setJMenuBar(new TMenuBar(m_Settings)); // create the menubar


        // add the drawArea to frame
        add(drawArea);

        // add the colorArea to frame
        add(colorArea);

        // add buttons to frame
        toolBoxInit();

        /**
         * Listeners:
         * In this section, all the listeners for buttons and panels are updated
         * here. There are listeners for the drawArea, colorArea, sizeSelection
         * Buttons, and MouseEvents
         */

        addComponentListener(new ComponentAdapter()
        {
            /**
             * This is for clearing the screen. I set the bounds to detect the 
             * actual windows placement. 
             */
            @Override
            public void componentMoved(ComponentEvent e)
            {        
                // bounds checking on the x axis to the right: current x > 570 and < 700
                // if it is then set mX1 to true
                if (e.getComponent().getX() > 570 && e.getComponent().getX() < 700)
                {
                    mX1 = true;
                } 
                // bounds checking on the x axis to the left: current x < 470 and > 200
                // if it is then set mX2 to true
                if (e.getComponent().getX() < 470 && e.getComponent().getX() > 200)
                {
                    mX2 = true;
                }
                // bounds checking on the y axis to the right: current y >  200 and < 300
                // if it is then set mY1 to true
                if (e.getComponent().getY() > 200 && e.getComponent().getY() < 300)
                    mY1 = true;
                // bounds checking on the y axis to the right: current y >  500 and < 700
                // if it is then set mY2 to true
                if (e.getComponent().getY() > 500 && e.getComponent().getY() < 700)
                    mY2 = true;
                // if all are set to true then the screen will be cleared
                if (mX1 == true && mX2 == true && mY1 == true && mY2 == true)
                {
                    drawArea.clearshapeList();
                    drawArea.setButtonStatus("none");
                    mX1 = false; mX2 = false; mY1 = false; mY2 = false;
                }
            }
        });
           
        drawArea.addMouseMotionListener(new MouseAdapter()
        {
            // update the coordinate system
            @Override
            public void mouseMoved(MouseEvent e)
            {
                x = e.getX();
                y = e.getY();
                xy.setText("X: " + x + " " + "Y: " + y);
            }
            @Override
            public void mouseDragged(MouseEvent e)
            {
                x = e.getX();
                y = e.getY();
                xy.setText("X: " + x + " " + "Y: " + y);
            }
        });
        // listner for line button
        line.addActionListener(new ActionListener()
        {
           public void actionPerformed(ActionEvent e)
           {
               drawArea.setButtonStatus("line");
           }
        });
        // listener for rectangle button
        rectangle.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                drawArea.setButtonStatus("rect");
            }
        });
        // listener for circle button
        circle.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                drawArea.setButtonStatus("circle");
            }
        });
        // listner for triangle button
        triangle.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                drawArea.setButtonStatus("triangle");
            }
        });
        // listener for brush button
        brush.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                drawArea.setButtonStatus("brush");
            }
        });
        // listener for select button
        select.addActionListener(new ActionListener()
        {
           public void actionPerformed(ActionEvent e)
           {
               drawArea.setButtonStatus("select");
           }
        });
        // listener for filled  radio button
        filled.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                non_filled.setSelected(false);
                drawArea.setFill("fill");
            }
        });
        // listener for non fill radio button
        non_filled.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                filled.setSelected(false);
                drawArea.setFill("nofill");
            }
        });
        // listener for red panel
        red.addMouseListener(new MouseAdapter()
        {
            @Override
            public void mouseReleased(MouseEvent e)
            {
                colorArea.setBackground(Color.RED);
                sizeOne.setBackground(Color.RED);
                sizeTwo.setBackground(Color.RED);
                sizeThree.setBackground(Color.RED);
                drawArea.setColor("red");
            }
        });
        // listener for blue panel
        blue.addMouseListener(new MouseAdapter()
        {
            @Override
            public void mouseReleased(MouseEvent e)
            {
                colorArea.setBackground(Color.BLUE);
                sizeOne.setBackground(Color.BLUE);
                sizeTwo.setBackground(Color.BLUE);
                sizeThree.setBackground(Color.BLUE);
                drawArea.setColor("blue");                
            }
        });
        // listener for green panel
        green.addMouseListener(new MouseAdapter()
        {
            @Override
            public void mouseReleased(MouseEvent e)
            {
                colorArea.setBackground(Color.GREEN);
                sizeOne.setBackground(Color.GREEN);
                sizeTwo.setBackground(Color.GREEN);
                sizeThree.setBackground(Color.GREEN);
                drawArea.setColor("green");
                
            }
        });
        // listener for black panel
        black.addMouseListener(new MouseAdapter()
        {
            @Override
            public void mouseReleased(MouseEvent e)
            {
                colorArea.setBackground(Color.BLACK);
                sizeOne.setBackground(Color.BLACK);
                sizeTwo.setBackground(Color.BLACK);
                sizeThree.setBackground(Color.BLACK);
                drawArea.setColor("black");
                
            }
        });
        // listener for sizeOne panel
        sizeOne.addMouseListener(new MouseAdapter()
        {
            @Override
            public void mouseReleased(MouseEvent e)
            {
                drawArea.setBrushSize(10);
            }
        });
        // listener for sizeTwo panel
        sizeTwo.addMouseListener(new MouseAdapter()
        {
            @Override
            public void mouseReleased(MouseEvent e)
            {
                drawArea.setBrushSize(20);
            }
        });
        // listener for sizeThree panel
        sizeThree.addMouseListener(new MouseAdapter()
        {
            @Override
            public void mouseReleased(MouseEvent e)
            {
                drawArea.setBrushSize(30);
            }
        });
    }

    /**
     * This section is designed to manually create the GUI. It places all the
     * panels and buttons(radion as well) on to the screen 
     */
    
    public void toolBoxInit()
    {
        // add tool text to frame
        {
            toolText.setSize(75,20);
            toolText.setLocation(35,0);
            add(toolText);
        }
        // add line to frame
        {            
            line.setSize(20, 20);
            line.setLocation(20,30);
            add(line);
        }

        // add rectangle to frame
        {            
            rectangle.setSize(20,20);
            rectangle.setLocation(60, 30);
            add(rectangle);
        }

        // add circle to frame
        {            
            circle.setSize(20,20);
            circle.setLocation(20,60);
            add(circle);
        }

        // add triangle to frame
        {            
            triangle.setSize(20,20);
            triangle.setLocation(60, 60);
            add(triangle);
        }

        // add brush to frame
        {            
            brush.setSize(20, 20);
            brush.setLocation(20, 90);
            add(brush);
        }
        {
            select.setSize(20,20);
            select.setLocation(60,90);
            add(select);
        }

        // add coordinate text
        {
            xyText.setSize(75,25);
            xyText.setLocation(12,490);
            xyText.setVisible(true);
            add(xyText);
        }
        // add mouse coordinates
        {
            xy.setSize(75,50);
            xy.setLocation(12, 500);
            xy.setVisible(true);
            add(xy);
        }
        // add radio buttons
        {
            // filled
            filled.setSize(75,25);
            filled.setText("Solid");
            filled.setLocation(0,150);
            filled.setSelected(false);
            add(filled);

            // non filled
            non_filled.setSize(85,25);
            non_filled.setText("No Fill");
            non_filled.setLocation(0,175);
            non_filled.setSelected(true);
            add(non_filled);
        }

        // color text
        {
            colorText.setSize(75,25);
            colorText.setLocation(32,225);
            add(colorText);
        }
        // colors
        {
            red.setSize(20,20);
            red.setLocation(20, 250);
            red.setBackground(Color.RED);
            add(red);

            blue.setSize(20,20);
            blue.setLocation(60, 250);
            blue.setBackground(Color.BLUE);
            add(blue);

            green.setSize(20,20);
            green.setLocation(20, 290);
            green.setBackground(Color.GREEN);
            add(green);


            black.setSize(20,20);
            black.setLocation(60, 290);
            black.setBackground(Color.BLACK);
            add(black);
        }
        // textSize title
        {
            textSize.setLocation(11, 375);
            textSize.setSize(100,25);
            add(textSize);
        }
        // size one
        {
            sizeOne.setLocation(15, 430);
            sizeOne.setSize(10,10);
            sizeOne.setBackground(Color.BLACK);
            add(sizeOne);
        }
        // size two
        {
            sizeTwo.setLocation(35,415);
            sizeTwo.setSize(20,20);
            
            sizeTwo.setBackground(Color.BLACK);
            add(sizeTwo);
        }
        // size three
        {
            sizeThree.setLocation(65,400);
            sizeThree.setSize(30,30);
            sizeThree.setBackground(Color.BLACK);
            add(sizeThree);
        }
    }
}
